package com.ruoyi.web.controller.system;

import java.util.List;

import com.ruoyi.common.utils.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TestCase;
import com.ruoyi.system.service.ITestCaseService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * test_caseController
 * 
 * @author ruoyi
 * @date 2022-09-27
 */
@Controller
@RequestMapping("/system/test_case")
public class TestCaseController extends BaseController
{
    private String prefix = "system/test_case";

    @Autowired
    private ITestCaseService testCaseService;

    @RequiresPermissions("system:test_case:view")
    @GetMapping()
    public String test_case()
    {
        return prefix + "/test_case";
    }

    /**
     * 查询test_case列表
     */
    @RequiresPermissions("system:test_case:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TestCase testCase)
    {
        startPage();
        List<TestCase> list = testCaseService.selectTestCaseList(testCase);
        return getDataTable(list);
    }

    /**
     * 导出test_case列表
     */
    @RequiresPermissions("system:test_case:export")
    @Log(title = "test_case", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TestCase testCase)
    {
        List<TestCase> list = testCaseService.selectTestCaseList(testCase);
        ExcelUtil<TestCase> util = new ExcelUtil<TestCase>(TestCase.class);
        return util.exportExcel(list, "test_case数据");
    }

    /**
     * 新增test_case
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        mmap.put("TestPlan", testCaseService.selectTestPlanName());
        return prefix + "/add";
    }

    /**
     * 新增保存test_case
     */
    @RequiresPermissions("system:test_case:add")
    @Log(title = "test_case", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TestCase testCase)
    {
        return toAjax(testCaseService.insertTestCase(testCase));
    }

    /**
     * 修改test_case
     */
    @RequiresPermissions("system:test_case:edit")
    @GetMapping("/edit/{caseId}")
    public String edit(@PathVariable("caseId") Long caseId, ModelMap mmap)
    {
        TestCase testCase = testCaseService.selectTestCaseByCaseId(caseId);
        mmap.put("testCase", testCase);
        return prefix + "/edit";
    }

    /**
     * 修改保存test_case
     */
    @RequiresPermissions("system:test_case:edit")
    @Log(title = "test_case", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TestCase testCase)
    {
        return toAjax(testCaseService.updateTestCase(testCase));
    }

    /**
     * 删除test_case
     */
    @RequiresPermissions("system:test_case:remove")
    @Log(title = "test_case", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(testCaseService.deleteTestCaseByCaseIds(ids));
    }

    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<TestCase> util = new ExcelUtil<TestCase>(TestCase.class);
        List<TestCase> testCaseList = util.importExcel(file.getInputStream());

        for(int i = 0;i < testCaseList.size();i++) {
            TestCase testCase = testCaseList.get(i);
            testCaseService.insertTestCase(testCase);
        }
        return AjaxResult.success();
    }

    /**
     * 下载模板
     */
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate()
    {
        ExcelUtil<TestCase> util = new ExcelUtil<TestCase>(TestCase.class);
        return util.importTemplateExcel("测试用例表");
    }

    /*@GetMapping("/chart")
    @ResponseBody
    public int[] chart() {
        Long sysUserId = ShiroUtils.getSysUser().getUserId();
        return testCaseService.chart(sysUserId);
    }*/
}
